package com.cmc.cloud.cmclink.doc.controller.admin.special;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.bdt.api.base.dto.resp.TerminalListByCodeRespDTO;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.TerminalDgTeuLimitedErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.TerminalDgTeuLimitedConvert;
import com.cmc.cloud.cmclink.doc.entity.TerminalDgTeuLimitedDO;
import com.cmc.cloud.cmclink.doc.rpcservice.TerminalTransferService;
import com.cmc.cloud.cmclink.doc.service.TerminalDgTeuLimitedService;
import com.cmc.cloud.cmclink.doc.vo.special.terminaldgteulimitedvo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;

import cn.hutool.core.collection.CollectionUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 港口各码头危险品单船作业限量维护前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 港口各码头危险品单船作业限量维护")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/special/teu-limited")
public class TerminalDgTeuLimitedController {

    @Resource
    private TerminalDgTeuLimitedService terminalDgTeuLimitedService;

    @Resource
    private TerminalTransferService terminalTransferService;

    @Operation(summary = "创建港口各码头危险品单船作业限量维护")
    @PostMapping("/create")
    public CommonResult<Void>
        createTerminalDgTeuLimited(@Valid @RequestBody TerminalDgTeuLimitedCreateReqVO createReqVO) {
        terminalDgTeuLimitedService.createTerminalDgTeuLimited(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新港口各码头危险品单船作业限量维护")
    @PostMapping("/update")
    public CommonResult<Void>
        updateTerminalDgTeuLimited(@Valid @RequestBody TerminalDgTeuLimitedUpdateReqVO updateReqVO) {
        terminalDgTeuLimitedService.updateTerminalDgTeuLimited(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除港口各码头危险品单船作业限量维护")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deleteTerminalDgTeuLimited(@PathVariable("id") Long id) {
        terminalDgTeuLimitedService.deleteTerminalDgTeuLimited(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得港口各码头危险品单船作业限量维护")
    @GetMapping("/get/detail/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @UseResponsePropertyAdvice
    public CommonResult<TerminalDgTeuLimitedRespVO> getTerminalDgTeuLimited(@PathVariable("id") Long id) {
        TerminalDgTeuLimitedDO terminalDgTeuLimited = terminalDgTeuLimitedService.getTerminalDgTeuLimited(id);
        if (terminalDgTeuLimited == null) {
            throw ServiceExceptionUtil.exception(TerminalDgTeuLimitedErrorCodeConstants.TERMINAL_DG_TEU_NOT_EXISTED);
        }
        TerminalDgTeuLimitedRespVO respVO = TerminalDgTeuLimitedConvert.INSTANCE.convert(terminalDgTeuLimited);
        List<String> terminalCodes = new ArrayList<>();
        terminalCodes.add(terminalDgTeuLimited.getTerminalCode());
        List<TerminalListByCodeRespDTO> terminalListByCodeRespDTOList = terminalTransferService.queryTerminalByCode(terminalCodes);
        if (!CollectionUtil.isEmpty(terminalListByCodeRespDTOList)) {
            respVO.setTerminalNameCn(terminalListByCodeRespDTOList.get(0).getTerminalNameCn());
        }
        return CommonResult.success(respVO);
    }

    @Operation(summary = "获得港口各码头危险品单船作业限量维护分页")
    @PostMapping("/page")
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<TerminalDgTeuLimitedRespVO>>
        getTerminalDgTeuLimitedPage(@Valid @RequestBody TerminalDgTeuLimitedPageReqVO pageVO) {
        PageResult<TerminalDgTeuLimitedDO> pageResult = terminalDgTeuLimitedService.getTerminalDgTeuLimitedPage(pageVO);
        List<TerminalDgTeuLimitedRespVO> results =
            TerminalDgTeuLimitedConvert.INSTANCE.convertList(pageResult.getList());

        List<String> terminalCodes =
            results.stream().map(TerminalDgTeuLimitedRespVO::getTerminalCode).collect(Collectors.toList());
        List<TerminalListByCodeRespDTO> terminalListByCodeRespDTOList = terminalTransferService.queryTerminalByCode(terminalCodes);
        results.forEach(result -> terminalListByCodeRespDTOList.forEach(resp -> {
            if (result.getTerminalCode().equals(resp.getTerminalCode())) {
                result.setTerminalNameCn(resp.getTerminalNameCn());
            }
        }));

        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "是否存在交叉生效日期, 存在返回true, 不存在返回false")
    @PostMapping("/date/overlap")
    public CommonResult<Boolean> dateOverlap(@Valid @RequestBody TerminalDgTeuLimitedBaseVO baseVo) {
        Boolean overlap = terminalDgTeuLimitedService.dateOverlap(baseVo);
        return CommonResult.success(overlap);
    }

    @Operation(summary = "批量更新港口各码头危险品单船作业限量失效时间")
    @GetMapping("/update/endDate")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @Parameter(name = "endDate", description = "结束日期", required = true, example = "2020-01-01")
    public CommonResult<Void> updateEndDates(@RequestParam("ids") List<Long> ids,
        @RequestParam("endDate") LocalDate endDate) {
        terminalDgTeuLimitedService.updateEndDates(ids, endDate);
        return CommonResult.success();
    }

}
